/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import com.toedter.calendar.JDateChooser;
import java.awt.Component;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author Canh
 */
public class FPanel extends JPanel {

    Component[] hiddenList;
    Component[] showList;
    Component[] editList;

    public Component[] getHiddenList() {
        return hiddenList;
    }

    public void setHiddenList(Component[] hiddenList) {
        this.hiddenList = hiddenList;

    }

    public FPanel() {
        hiddenList = new Component[0];
        showList = new Component[0];
        editList = new Component[0];
    }

    public Component[] getEditList() {
        return editList;
    }

    public void setEditList(Component[] editList) {
        this.editList = editList;
    }

    public Component[] getShowList() {
        return showList;
    }

    public void setShowList(Component[] showList) {
        this.showList = showList;
    }

    public void setEditFunction(boolean isEdit) {

        for (int i = 0; i < this.getComponents().length; i++) {
            if (this.getComponents()[i] instanceof JButton) {
                this.getComponent(i).setEnabled(isEdit);
            }
        }

    }

    public void refresh() {
        for (int i = 0; i < this.getComponents().length; i++) {
            if (this.getComponents()[i] instanceof JTextField) {
                ((JTextField) this.getComponents()[i]).setText("");
            } else if (this.getComponents()[i] instanceof JComboBox) {
                try {
                    ((JComboBox) this.getComponents()[i]).setSelectedIndex(-1);
                } catch (Exception ex) {
                }
            } else if (this.getComponents()[i] instanceof JTextArea) {
                ((JTextArea) this.getComponents()[i]).setText("");
            } else if (this.getComponents()[i] instanceof JDateChooser) {
                ((JDateChooser) this.getComponents()[i]).setDate(new Date());
            }
        }

        for (int i = 0; i < editList.length; i++) {
            if (editList[i] instanceof JTextField) {
                ((JTextField) editList[i]).setText("");
            } else if (editList[i] instanceof JComboBox) {
                try {
                    ((JComboBox) editList[i]).setSelectedIndex(-1);
                } catch (Exception ex) {
                }
            } else if (editList[i] instanceof JTextArea) {
                ((JTextArea) editList[i]).setText("");
            } else if (editList[i] instanceof JDateChooser) {
                ((JDateChooser) editList[i]).setDate(new Date());
            }
        }
    }

    public void setEditValue(boolean editable) {
        for (int i = 0; i < this.getComponents().length; i++) {
            if (this.getComponents()[i] instanceof JTextField) {
                ((JTextField) this.getComponents()[i]).setEditable(editable);
            } else if (this.getComponents()[i] instanceof JComboBox) {

                ((JComboBox) this.getComponents()[i]).setEditable(editable);

            } else if (this.getComponents()[i] instanceof JTextArea) {
                ((JTextArea) this.getComponents()[i]).setEditable(editable);
            } else if (this.getComponents()[i] instanceof JDateChooser) {
                ((JDateChooser) this.getComponents()[i]).setEnabled(editable);
            }
        }

        for (int i = 0; i < editList.length; i++) {
            if (editList[i] instanceof JTextField) {
                ((JTextField) editList[i]).setEditable(editable);
            } else if (editList[i] instanceof JComboBox) {
                try {
                    ((JComboBox) editList[i]).setEditable(editable);
                } catch (Exception ex) {
                }
            } else if (editList[i] instanceof JTextArea) {
                ((JTextArea) editList[i]).setEditable(editable);
            } else if (editList[i] instanceof JDateChooser) {
                ((JDateChooser) editList[i]).setEnabled(editable);
            }
        }

        editable = false;
        if (hiddenList != null) {
            for (int i = 0; i < hiddenList.length; i++) {
                if (hiddenList[i] instanceof JTextField) {
                    ((JTextField) hiddenList[i]).setEditable(editable);
                } else if (hiddenList[i] instanceof JComboBox) {

                    ((JComboBox) hiddenList[i]).setEditable(editable);

                } else if (hiddenList[i] instanceof JTextArea) {
                    ((JTextArea) hiddenList[i]).setEditable(editable);
                } else if (hiddenList[i] instanceof JDateChooser) {
                    ((JDateChooser) hiddenList[i]).setEnabled(editable);
                }
            }
        }
        editable = true;
        if (showList != null) {
            for (int i = 0; i < showList.length; i++) {
                if (hiddenList[i] instanceof JTextField) {
                    ((JTextField) hiddenList[i]).setEditable(editable);
                } else if (hiddenList[i] instanceof JComboBox) {

                    ((JComboBox) hiddenList[i]).setEditable(editable);

                } else if (hiddenList[i] instanceof JTextArea) {
                    ((JTextArea) hiddenList[i]).setEditable(editable);
                } else if (hiddenList[i] instanceof JDateChooser) {
                    ((JDateChooser) hiddenList[i]).setEnabled(editable);
                }
            }
        }

    }
}
